const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastlist: [],
    srctype: "门店状态",
    customItem: '全部',
    array: [
      { 'name': '未提交', state: 0 },
      { 'name': '已审核', state: 100 },
      { 'name': '提交审核', state: 10 },
      { 'name': '初审通过', state: 20 },
      { 'name': '审批退回', state: 5 },
      { 'name': '商户申请', state: 30 },
      { 'name': '商户申请失败', state: 15 },
      { 'name': '门店失效', state: -10 },
    ],
    index: 0,
    key: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userinfo = wx.getStorageSync('userinfo');
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight - 150,
        })
      },
    })
   
    that.chushi()
  },
  chushi:function(){
    var that = this;
    var depid = wx.getStorageSync('depid');
    var mebid = wx.getStorageSync('mebid');
    var where = '';
    if (depid) {
      where += " and DepID =" + depid +" and MebID <> "+mebid;
    }
    var apinameIsMeb = "/BO/AP_ShopMembers/Query";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        where: where
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        // this.globalData.userInfo = JSON.parse(res.data);
        console.log(res);
        if (res.data.data.length > 0) {
          var star = res.data.data[0].StarLevel ? res.data.data[0].StarLevel : "";
          that.setData({
            lastlist: res.data.data,
            newinfo: res.data.data[0],
            star: star,
            starnone: 5 - star,
          })
      
        }
      }
    })
  },
  addson: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        var datas = res.result.split('&');
        var mebarr = datas[0].split(':');
        if (mebarr[0] == 'mebid' && mebarr[1] != '') {
          wx.showModal({
            title: '',
            content: '确定添加该账号为门店子用户？',
            success: function (res) {
              if (res.confirm) {
                var datas = {};
                var data = {};
                data.MebID = mebarr[1];
                data.DepID = wx.getStorageSync('depid');

                datas.data = JSON.stringify(data);
                var datacall = function (res) {
                  console.log(res);
                  // if (res.data.code == 0) {
                  //   wx.showModal({
                  //     title: '',
                  //     content: '添加成功~',
                  //   })
                    
                  // }
                  that.chushi();
                }
                var res2 = post.getdata("/BO/AP_ShopMembers/Add", datas, datacall);
              } else if (res.cancel) {
                return;
              }
            }
          })
        } else {
          wx.showModal({
            title: '',
            content: '不是符合规定的二维码',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  del:function(e){
    var that = this
    var mebid = e.currentTarget.dataset.mebid;
    var index = e.currentTarget.dataset.index;
    var depid = e.currentTarget.dataset.depid;
    console.log(e);
    wx.showModal({
      title: '提示',
      content: '是否确定删除该联系人！！！',
      success: function (res) {
        if (res.confirm) {
          //获取列表中要删除项的下标   
          var lastlist = that.data.lastlist;
          //移除列表中下标为index的项  
          lastlist.splice(index, 1);
          //更新列表的状态  
          that.setData({
            lastlist: lastlist
          });
          //删除联系人
          var datas = {};
          datas.MebID = mebid;
          datas.DepID = depid;
          var datacall = function (res) {
            console.log(res);
          }
          var res2 = post.getdata("/BO/AP_ShopMembers/Delete", datas, datacall);
        } 
      }
    })
  },
  newdata:function(){
    
  }
})